use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json;

#[derive(Serialize, Deserialize)]
struct OllamaMessage {
    model: String,
    prompt: String,
    stream: bool,
}

#[derive(Serialize, Deserialize)]
struct OllamaResponse {
    model: String,
    created_at: String,
    response: String,
}

pub fn generate_commit_message_by_git_diff(patch_content: &str) -> Result<String> {
    let url = "http://localhost:11434/api/generate";
    let message = OllamaMessage {
        model: "llama3.1".to_string(),
        prompt: patch_content.to_string(),
        stream: false,
    };
    let client = reqwest::blocking::Client::new();
    let response = client.post(url).json(&message).send()?;
    let final_comment =
        serde_json::from_str(response.text()?.as_str()).map(|res: OllamaResponse| res.response);

    Ok(final_comment?)
}
